package com.longway.common.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.longway.common.db.id.IIdFactory;
import com.longway.common.db.id.IdManager;
import com.longway.common.db.id.OracleSeqIdFactory;

public class DbService {
	
	public static Object getId(){
		return IdManager.me().getDefaultIdFactory().getId();
	}
	
	private static IIdFactory getDefaultFactory(){
		return IdManager.me().getDefaultIdFactory();
	}
	
	public static boolean save(String tableName,Record record){
		Object id = getDefaultFactory().getId();
		record.set("id", id);
		return Db.save(tableName, record);
	}
	
	public static boolean save(String tableName,Record record,String primaryKey){
		Object id = getDefaultFactory().getId();
		record.set(primaryKey, id);
		return Db.save(tableName, record);
	}
	
	public static int[] batchSave(String tableName,List<Record> recordList, int batchSize){
		IIdFactory ify = getDefaultFactory();
		if(ify instanceof OracleSeqIdFactory){
			Object id = ify.getId();
			for(Record record : recordList){
				record.set("id", id);
			}
		}
		return Db.batchSave(tableName, recordList, batchSize);
	}
	
	public static int[] batchUpdate(String tableName,List<Record> recordList, int batchSize){
		return Db.batchUpdate(tableName, recordList, batchSize);
	}


}
